<template>
  <div class="jump-type">
    <div class="search_box">
      <el-form :inline="true" :model="params" class="demo-form-inline">
        <el-form-item label="商品名称">
          <el-input v-model="params.param" size="mini" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item label="商品分类">
          <el-select v-model="params.type" placeholder="请选择" size="mini">
            <el-option label="全部" value=""></el-option>
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" size="mini" icon="el-icon-search">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="loading" border :data="tableData" ref="tables" rowKey="id"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" :reserve-selection="true"></el-table-column>
      <el-table-column prop="id" fixed="left" width="180" label="商品编号" align="center"></el-table-column>
      <el-table-column prop="name" label="商品名称" :show-overflow-tooltip="true" align="center"></el-table-column>
      <el-table-column label="商品图片" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <template v-if="scope.row.fileList.length > 0">
            <el-image style="height: 60px; border-radius: 5px;" :src="scope.row.fileList[0].fileUrl"
              :preview-src-list="getPreviewList(scope.row.fileList)"></el-image>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="到手价（¥）" :show-overflow-tooltip="true" align="center"></el-table-column>
      <el-table-column prop="brokerageRatio" label="佣金比（%）" align="center"></el-table-column>
      <el-table-column prop="brokerage" label="佣金（¥）" align="center"></el-table-column>
      <el-table-column prop="readCount" label="浏览量" align="center"></el-table-column>
      <el-table-column prop="clickCount" label="点击量" align="center"></el-table-column>
      <el-table-column prop="shopName" label="所属门店" align="center"></el-table-column>
    </el-table>
    <div class="box-justify-between">
      <el-pagination @current-change="handleCurrentChange" :current-page.sync="params.pageNum"
        layout="prev, pager, next" :page-size="params.pageSize" :total="total">
      </el-pagination>

      <el-button type="primary" @click="selectRow" size="mini">({{ selectRowsList.length }}) 确定</el-button>
    </div>
  </div>
</template>

<script>
import { goodsList } from '@/api/tktk/goods'
import { classifyList } from '@/api/tktk/classify'
import { EventBus } from '@/utils/event-bus'
export default {
  name: "selectGoodsMultiple",
  props: {
    list: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      tableData: [],
      selectRowsList: [],
      total: 0,
      loading: false,
      options: [],
      searchParams: {
        pageNum: 1,
        pageSize: -1
      },
      params: {
        param: '',
        type: '',
        feature: -1,
        pageNum: 1,
        pageSize: 10,
      }
    }
  },

  methods: {
    selectRow() {
      if (this.selectRowsList.length > 0) {
        this.$emit('transfer', this.selectRowsList)
      } else {
        this.$message.warning('请选择商品')
      }
    },
    classifyList() {
      classifyList(this.searchParams).then(res => {
        if (res.code == 200) {
          this.options = res.rows || []
        }
      })
    },
    getPreviewList(fileList) {
      let list = []
      fileList.map(item => {
        list.push(item.fileUrl)
      })
      return list
    },

    handleSelectionChange(val) {
      this.selectRowsList = [...val]
    },

    getGoodsList() {
      this.loading = true
      goodsList(this.params).then(res => {
        if (res.code === 200) {
          this.tableData = res.rows || []
          this.total = res.total || 0
          this.loading = false
        }
      })
    },

    handleCurrentChange(page) {
      this.params.pageNum = page
      this.getGoodsList()
    },

    onSubmit() {
      this.params.pageNum = 1
      this.getGoodsList()
    },

  },

  mounted() {
    this.classifyList()
    this.getGoodsList()

    console.log(this.list.length, 'list')
    this.list.map(item => {
      this.$refs.tables.toggleRowSelection(item, true)
    })
  }
}
</script>

<style lang="scss" scoped>
.jump-type {
  width: 100%;
  padding: 20px;
}
</style>
